package com.google.bling.client.widget;

import java.util.List;
import java.util.Map;

import com.google.bling.client.Util;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class AddExpensePopup extends DialogBox {
  public interface Listener {
    public void onExpenseAdded(String description, int amount,
        Map<String, Integer> owedAmounts);
  }

  private VerticalPanel vPanel;
  private TextBox descriptionTextBox = new TextBox();
  private TextBox amountTextBox = new TextBox();
  private Listener listener;
  private ExpenseRetrievalGrid expenseGrid;
  private Button cancelButton;
  private Button doneButton;
  private Chart chart = new Chart();

  public AddExpensePopup(final String loggedInUser, List<String> otherUsers) {
    List<String> allUsers = Util.list(loggedInUser);
    allUsers.addAll(otherUsers);
    setAnimationEnabled(true);
    vPanel = new VerticalPanel();
    add(vPanel);
    setText("Enter your expense here!");
    vPanel.add(new Label("Description"));
    vPanel.add(descriptionTextBox);
    vPanel.add(new Label("Amount"));
    vPanel.add(amountTextBox);
    vPanel.add(expenseGrid = new ExpenseRetrievalGrid(allUsers));
    HorizontalPanel buttonPanel = new HorizontalPanel();
    buttonPanel.add(doneButton = new Button("Done"));
    buttonPanel.add(cancelButton = new Button("Cancel"));
    vPanel.add(buttonPanel);
    
    amountTextBox.addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        onAmountChanged();
      }
    });

    amountTextBox.addKeyboardListener(new KeyboardListenerAdapter() {
      @Override
      public void onKeyUp(Widget sender, char keyCode, int modifiers) {
        onAmountChanged();
      }
    });

    cancelButton.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        hide();
      }
    });

    doneButton.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        hide();
        if (listener != null) {
          Map<String, Integer> amounts = expenseGrid.getAmounts();
          int amount = Util.parseInt(amountTextBox.getText());
          listener.onExpenseAdded(descriptionTextBox.getText(),
              amount, amounts);
        }
      }
    });

    doneButton.setEnabled(false);
  }

  public void onAmountChanged() {
    if (!validate()) {
      return;
    }
    expenseGrid.setTotalAmount(Util.parseInt(amountTextBox.getText()));
  }

  public boolean validate() {
    if (!Util.isNumber(amountTextBox.getText())) {
      error("Invalid amount");
      return false;
    }
    doneButton.setEnabled(true);
    return true;
  }

  private void error(String msg) {
    doneButton.setEnabled(false);
    // Nowhere to put this message at the moment.
  }

  public void clearInput() {
    amountTextBox.setText("");
    descriptionTextBox.setText("");
    expenseGrid.clearInput();
    doneButton.setEnabled(false);
  }

  public void setListener(Listener listener) {
    this.listener = listener;
  }

  @Override
  public void show() {
    clearInput();
    super.show();
  }
}
